package com.cloud.netty.ProtocolTcp;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;

/**
 * @program: netty
 * @description:
 * @author: Cloud
 * @create: 2022/4/13 11:19:28
 */
public class Proclient {

    public static void main(String[] args) throws Exception{
        EventLoopGroup group = new NioEventLoopGroup();

        try {
            Bootstrap bootstrap = new Bootstrap();
            bootstrap.group(group).channel(NioSocketChannel.class)
                    .handler(new ProClientInitializer());

            // sync() 阻塞  等待连接相关信息初始化完成了，则通过
            ChannelFuture channelFuture = bootstrap.connect("localhost", 8000).sync();
            //  该sync() 阻塞 当监听到其他线程触发了关闭的事件的时候，则才来调用关闭的操作，执行到finally的代码块上面去。否则一直阻塞，进入wait的状态，连接正常运行。
            channelFuture.channel().closeFuture().sync();
        } finally {
            group.shutdownGracefully();
        }

    }
}
